//{=================================================================================
//! @file    SolveSquare.cpp
//! @date    "__DATE__" "__TIME__"
//! @author  Kirill Chumachkov  <kchumachkov@mail.ru>
//!
//! Solution of the problem
//!                        ax^2 + bx + c = 0
//!
//! @par     "Condition of the problem"
//}=================================================================================

#include <stdio.h>
#include <math.h>

#define sqr(x) x*x


double sum(double *x, double *y);


int solvesquare(double a, double b, double c, double *x1, double *x2);


int main()
{
    double a = 0, b = 0, c = 0,
            x1 = 0, x2 = 0;

    printf("Hello, world! \n It is SolveSquareCode -- SSC 1.0 by kirillch\n");
    printf("Input a, b, c >\n");

    scanf("%lg%lg%lg", &a, &b, &c);

    if (solvesquare(a, b, c, &x1, &x2) == -2)
        printf("WTF?!\n\n");
    if (solvesquare(a, b, c, &x1, &x2) == -1)
        printf("Any roots, fellow!\n\n");
    if (solvesquare(a, b, c, &x1, &x2) == 0)
        printf("No roots (at all)\n\n");
    if (solvesquare(a, b, c, &x1, &x2) == 1)
        printf("One root\n x1 = %lg\n\n", x1);
    if (solvesquare(a, b, c, &x1, &x2) == 2)
        printf("Two roots\n x1 = %lg, x2 = %lg\n\n", x1, x2);

    return 0;
}


//{=====================================================================================
//!solvesquare - solve a square or linear equation specified by its coefficients
//!              and return the number of the roots.
//{=====================================================================================


int solvesquare(double a, double b, double c, double *x1, double *x2)
{
    if (a == 0)
    {
        if (b == 0)
        {
            if (c == 0)
                return -1;
            else return -2;
        }
        else
            if (c == 0)
                return 0;
            else
            {
                *x1 = - c / b;
                return 1;
            };
    }
    else
    {
        double d = b * b - 4 * a * c;
        if (d < 0)
            return 0;
        else
        {
            d = sqrt(d);

            *x1 = (- b + d) /2/a;
            *x2 = (- b - d) /2/a;

            if (d == 0)
                return 1;
            else
                return 2;

        };
    };
}


double sum(double *x, double *y)
{
    (*x)++;
    (*y)++;
    return (*x + *y);
}
